
#include "WeaponHud.h"
#include <GL\GL.h>

WeaponHud::WeaponHud(int x, int y, int w, int h, int sp)
{
	_x = x;
	_y = y;
	_w = w;
	_h = h;
	_sp = sp;
	
	gun1 = new CTexture;
	gun2 = new CTexture;
	gun3 = new CTexture;

	loadTexture(gun1, MEDIA_DIR "gun1.tga", GL_RGBA);
	loadTexture(gun2, MEDIA_DIR "gun2.tga", GL_RGBA);
	loadTexture(gun3, MEDIA_DIR "gun3.tga", GL_RGBA);
}

WeaponHud::~WeaponHud()
{
	delete gun1;
	delete gun2;
	delete gun3;
}

void WeaponHud::Render(int activeGun)
{
	int x = _x;
	int y = _y;

	glDepthMask(GL_FALSE);
	glEnable(GL_TEXTURE_2D);
	
	compareActiveGunAndSetColor(activeGun, 1);

	glBindTexture(GL_TEXTURE_2D, gun1->texID);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

	glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 1.0f);
		glVertex2i(x, y);
		glTexCoord2f(0.0f, 0.0f);
		glVertex2i(x, y + _h);
		glTexCoord2f(1.0f, 0.0f);
		glVertex2i(x + _w, y + _h);
		glTexCoord2f(1.0f, 1.0f);
		glVertex2i(x + _w, y);
	glEnd();

	x += _w + _sp;
	
	glBindTexture(GL_TEXTURE_2D, gun2->texID);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

	compareActiveGunAndSetColor(activeGun, 2);

	glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 1.0f);
		glVertex2i(x, y);
		glTexCoord2f(0.0f, 0.0f);
		glVertex2i(x, y + _h);
		glTexCoord2f(1.0f, 0.0f);
		glVertex2i(x + _w, y + _h);
		glTexCoord2f(1.0f, 1.0f);
		glVertex2i(x + _w, y);
	glEnd();

	x += _w + _sp;
	
	glBindTexture(GL_TEXTURE_2D, gun3->texID);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	
	compareActiveGunAndSetColor(activeGun, 3);

	glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 1.0f);
		glVertex2i(x, y);
		glTexCoord2f(0.0f, 0.0f);
		glVertex2i(x, y + _h);
		glTexCoord2f(1.0f, 0.0f);
		glVertex2i(x + _w, y + _h);
		glTexCoord2f(1.0f, 1.0f);
		glVertex2i(x + _w, y);
	glEnd();

	glDisable(GL_TEXTURE_2D);
	glDisable(GL_BLEND);
	glDepthMask(GL_TRUE);
	
	glPushMatrix();
	glLoadIdentity();
}

void WeaponHud::loadTexture(CTexture *texture, char* filename, int type)
{
	texture->LoadTexture(filename);

	glGenTextures(1, &texture->texID);
	glBindTexture(GL_TEXTURE_2D, texture->texID);
	
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	gluBuild2DMipmaps(GL_TEXTURE_2D, type == GL_RGBA ? 4 : 3, texture->width, texture->height, type, GL_UNSIGNED_BYTE, texture->data);
}

void WeaponHud::compareActiveGunAndSetColor(int activeGun, int gun)
{
	if(activeGun == gun)
	{
		glColor4f(0.0, 0.8, 0.0, 0.1f);
	}
	else
	{
		glColor4f(1.0f, 1.0f, 1.0f, 0.0f);
	}
}
